import {
  apiMode
} from '../../../utils/env'

import {
  authLogin,
  getOfferShareAssist
} from '../../../utils/api'
Page({
  data: {
    code: '',
    bugImg: 'https://cms.dafang24.com/MicroAppResource/images/assistance_2021/Zlbanner.png',
    isHelped: false,
    assistanceID: 134,
    beHelpUserID: 0,
    offerName:"2021毕业季",
    maskZindex: 10,
    userName:'name',
    num:1,
    type: 'market',
    maskZindex: 10,
    userName:'',
    Telephone:'',
    isNewUser:false
  },
  onLoad(e) { 
    this.setData({
      userName:e.Name||"",
      // Telephone:e.Telephone||""
    })
    if (e.userID) {
      this.setData({ beHelpUserID: e.userID });
    }
    let query = getApp().globalData.query || {};
    if (query.userID) {
      this.setData({ beHelpUserID: query.userID });
    }
    if (apiMode != 'build') {
      this.setData({
        assistanceID: apiMode == 'dev' ? 134 : 74
      });
    }
  },
  onReady() { 
    
  },
  onShow() {
    wx.login({
      success:(res)=>{
        this.setData({
          code: res.code
        })
      }
    })
  },
  async helpFriend () {
    wx.reLaunch({
      url: '/pages/rent/shortrent/shortrent',
    });
  },
  async getOfferShareAssistFn(){
    let beAssistUserID = this.data.beHelpUserID
    let offerIds = [139,140,141] // "5354,5353,5352'
    let data = {
      offerIds, 
      beAssistUserID: beAssistUserID
    };
    // console.log("getOfferShareAssistFn:" + JSON.stringify(data));
    let res = await getOfferShareAssist({
      data: data,
      // showError: false
    });
    console.log(res)
    if( res.Code==100 ){
      console.log(1111111)
      let isNewUser = this.data.isNewUser || false;
      if(isNewUser == false){
        this.setData({
          num:3
        })
      }
      if(isNewUser == true){
        this.setData({
          num:2
        })
      }
    }
    if(res.Code != 100){
      showError:true
      return
    }
    // console.log(res,'+++++++')
  },
  login: function (data) {
    let _this = this;
    let position = wx.getStorageSync('position')
    let _data = {}
    if (position) {
      _data = {
        LoginApp: 9,
        code: _this.data.code,
        encryptData: data.detail.encryptedData,
        iv: data.detail.iv,
        RefereeID: 0,
        lng: position.lng,
        lat: position.lat,
        CityName: position.CityName,
        CityCode: position.CityCode,
        RegisterResourceID: _this.data.assistanceID,// 活动Id
        BeAssistUserID: _this.data.beHelpUserID,
        RegisterResourceType: 1
      }
    } else {
      _data = {
        LoginApp: 9,
        code: _this.data.code,
        encryptData: data.detail.encryptedData,
        iv: data.detail.iv,
        RefereeID: 0,
        RegisterResourceID: _this.data.assistanceID,// 活动Id
        BeAssistUserID: _this.data.beHelpUserID,
        RegisterResourceType: 1
      }
    }
    
    if (_data.encryptData && _data.iv){
      authLogin({
        data: _data,
        isLoading: true,
        loadContent: "正在助力..."
      }).then( async res => {
        // console.log(res)
        if (res.Code == 100) {
          wx.setStorageSync('token', res.Message);
          getApp().globalData.token = res.Message;
          let isNewUser = (res.Data && res.Data.IsNewUser) || false;  // 是否新用户
         let telephone = res.Data.Telphone
          this.setData({ isNewUser: isNewUser, isHelped: true ,Telephone:telephone});
          // console.log(telephone,this.data.Telphone)
          await this.getOfferShareAssistFn();
        } else if (res.Code == 17203) {
          this.setData({
            setPhone: '',
            users: res.Data,
            isOldUser: true
          })
        } else if(res.Code == 17007){
          // this.login()
          console.log('111111')
        }
      })
    }
  },
   // 关闭按钮
   close(){
    wx.reLaunch({
      url: '/pages/rent/shortrent/shortrent',
    });
  }
});

